<?php

require_once 'Form.php';
require_once 'RequestHandler.php';
class FormHandler extends RequestHandler
{
	var $cfg=NULL;

	public function handle ()
	{
		$form = $this->getForm();
		disp_form($form);
	} // end of method d
	public function handle_POST ()
	{
		$form = $this->getForm();
		$form->process();
		if ($form->status === 'ok') {
			return $form->vhash;
		}
		disp_form($form);
	} // end of method do_POST

	public function getForm ()
	{
		if (!isset($this->form)) {
			$this->form = $this->buildForm($this->form_config);
		}
		return $this->form;
	} // end of method getForm
	public function buildForm ($form)
	{
		if (!isset($form['action'])) $form['action'] = $_SERVER['REQUEST_URI'];
		foreach ($form['fields'] as $fn =>& $fc) {
			if (method_exists($this, 'validate_'.$fn)) {
				$fc['validator'] = array($this, 'validate_'.$fn);
			}
			if (method_exists($this, 'default_'.$fn)) {
				$fc['default'] = array($this, 'default_'.$fn);
			}
		}
		if (!isset($form['buttons'])) {
			$form['buttons'] = array(
				'ok' => array(
					'type' => 'image',
					'title' => 'Установить',
					'src' => '../skins/platonica/images/title/button-install.gif',
				),
			);
		}
		$f = new Form($form);
		return $f;
	} // end of method buildForm
}
